<?php 

//require("../../functions.php");
require("../db.php");

echo getcwd() . "<br />";

$weekend = array();
$wk = array();
$weekend_classes = array();
$workweek_classes = array();
$event_info = array();
$content = array();
$temp_wrkweek = array();
$temp_wkend = array();
$data = array();
$workweek = array();

if(isset($_GET['action'])) {
	print $_GET['action'] . "<br />";
} else {
	print "you arent in any navigation for the week cal... <br />";
}

//store the event information based on the day this queries
//$events = mysql_query("SELECT users.fname, users.lname, cal.model_name, cal.assignment, cal.date FROM users INNER JOIN cal ON cal.id_users = users.id") or die(mysql_error());

$events = $db->query("SELECT users.fname, users.lname, cal.model_name, cal.assignment, cal.location, cal.date FROM users INNER JOIN cal ON cal.id_users = users.id");

$events->setFetchMode(PDO::FETCH_ASSOC); 

//$row = mysql_fetch_array($events) or die(mysql_error());

while($row = $events->fetch()) {
	$content[] = $row;
}

/*while($row = mysql_fetch_array($events, MYSQL_ASSOC)) {
	$content[] = $row;
}*/

//DO NOT INCLUDE in week cal render
$today = new DateTime(); //for today

$jan1 = new DateTime('01/01/2012');

$offset = 1 - $jan1->format('w');

$firstMonday = $jan1->add(new DateInterval("P{$offset}D"));

$firstWeek = (int) $firstMonday->format('W');

$dateTimeArg = "";

if (isset($_GET['fm'])) {
    $dateTimeArg = urldecode($_GET['fm']);
}

//echo urldecode($_GET['fm']);

if($dateTimeArg == "") {
	$start_date = new DateTime();
} else {
	$start_date = new DateTime($dateTimeArg); 
}

$wk = (int) $start_date->format('W');

if ($firstWeek <= 1) {
    --$wk;
}

$d = (int) $firstMonday->format('z') + (($wk) * 7); 

--$d;

$thisMonday = $firstMonday->add(new DateInterval("P{$d}D"));
$start_date = $thisMonday;


//start "orgin date"
//$start_date = new DateTime("monday this week"); 

if(isset($_GET['action'])) {
    $action = $_GET['action'];

    if ($action == "next") {
        $start_date = $start_date->add(new DateInterval('P1W'));
    }   

    if ($action == 'prev') {    
        $start_date = $start_date->sub(new DateInterval('P1W'));
    }
} 

//$start_date = DateTime::createFromFormat('Y-m-d', "2012-01-19");
//$start_date->modify("monday this week");
if(isset($_GET['yearID'])) {
	print "the active year on mini calendar is.." . $_GET['yearID'] . "<br /><br />";
} else {
	print "dude.. the mini calendar isnt even open..." . "<br /><br />";
}

print "<b>This is for the month of... " . $start_date->format('F') . "<br /> and today is.. " . $today->format('l,') . " the " . $today->format("jS") . "</b><br /> <br />";
//$next_week = clone $start_date;
$next_week = clone $start_date;
$next_week->add(new DateInterval("P1W"));

$weekperiod = new DatePeriod($start_date, new DateInterval('P1D'), $next_week);

foreach($weekperiod as $week) {
	//$tmp[] = $week->format('l - F, d');
	//$tmp[] = $week->format('Y-m-d');

	 if(strtolower($week->format('D')) == "sat" || strtolower($week->format('D')) == "sun") {

		//echo $week->format('m/d/Y - l') . '<br />';
	 	unset($data);

		//store mysql events from weekend into temp var
		foreach($content as $e) {

			if($e['date'] == $week->format('Y-m-d')) {
				
				$data[] = array(
					"date" => $e['date'],
					"type" => $e['assignment'],
					"name" => $e['model_name'],
					"location" => $e['location']
				);

			} else {

				$trash[] = $content;

			}

		}

		if(isset($data)) {

			$weekend[] = array(
				"label" => $week->format('l - F, d'),
				"class" => $week->format('l-d'),
				"db_date" => $week->format('Y-m-d'),
				"evnt" => $data
			);

		} else {

			$weekend[] = array(
				"label" => $week->format('l - F, d'),
				"class" => $week->format('l-d'),
				"db_date" => $week->format('Y-m-d')
			);
			
		}

	} else {
		
		//echo $week->format('m/d/Y - l') . '<br />';
		unset($data);
		
		foreach($content as $e) {

				
			if($e['date'] == $week->format('Y-m-d')) {
				
				$data[] = array(
					"date" => $e['date'],
					"type" => $e['assignment'],
					"name" => $e['model_name'],
					"location" => $e['location']
				);
				
			} else {

				//unset($data);

				//these are the dates that do not match the date in the week
				//so populate the trash array
				$trash = $content;
			}

		}

		if(isset($data)) {

			$workweek[] = array(
				"label" => $week->format('l - F, d'),
				"class" => $week->format('l-d'),
				"db_date" => $week->format('Y-m-d'),
				"evnt" => $data
			);

		} else {

			$workweek[] = array(
				"label" => $week->format('l - F, d'),
				"class" => $week->format('l-d'),
				"db_date" => $week->format('Y-m-d')
			);
			
		}

	}


} // end foreach


/*
foreach($workweek as $ww) {

	print $ww['label'] . "<br />";
	print $ww['class'] . "<br />";
	print "<br />";

	if(array_key_exists('evnt', $ww) && count($ww['evnt']) > 0) {

		if(count($ww['evnt']) > 0) {

			print "there are " . count($ww['evnt']) . " events on this date <br />";

		} 
	
	} else {

		print "theres NO content in evnt <br />";

	}

}*/


/*
print "<pre>";
	var_export($data);
print "</pre>";
*/
/*
print "<pre>";
	$week = array(
		"workweek" => $workweek,
		"weekend" => $weekend
	);

	print_r($week);
print "</pre>";
*/


/*
print "<pre>";
	//var_export($wk);
	//print_r($temp);
	//var_export($workweek);
	print_r($workweek);
	print "<br>";
	//print_r();
	//print_r($wk);
	//var_dump($wk);
print "</pre>";*/


//print "Today is.. " . $today->format('l');?>
<!--
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <a href="?action=prev&fm=<?php //echo $start_date->format('m/d/Y');?>">Previous</a>
        <a href="?action=next&fm=<?php //echo $start_date->format('m/d/Y');?>">Next</a>
    </body>
</html>
-->